import { configureStore } from "@reduxjs/toolkit";
import collapsedReducer from "./modules/collapsedStore";
import loadingreducer from "./modules/loadingStore";
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import { combineReducers } from "@reduxjs/toolkit";

// 配置persist
const persistConfig = {
  key: 'root',
  storage,
  whitelist: ['collapsed'], // 指定需要持久化的reducer
  blacklist: ['loading'] // 修正拼写错误: 'laoding' -> 'loading'
}

// 创建reducer对象 combineReducers是将两个reducer对象进行合并 现在使用configReducer进行合并 但是这里为了持久化做出让步
const rootReducer = combineReducers({
  collapsed: collapsedReducer,
  loading: loadingreducer
});

// 创建持久化reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);

const store = configureStore({
    reducer: persistedReducer,
    middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: ['persist/PERSIST'], // 忽略persist/PERSIST action的序列化检查
      },
    }),
})

const persistor = persistStore(store);

export {
    persistor,
    store
}